<template>
  <div class="addArticle">
    <el-header style="line-height:60px"><h2>用户查看</h2></el-header>
        <div class="card kjfs">
          <el-row>
            <el-col :span="24">
              <p class="title" style="padding:25px"><i class="fa fa-th-large fa-lg"></i>用户信息</p>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="18" :offset="2">
              <el-form ref="form" :rules="rules"
                                  :model="form" 
                                  label-width="120px" 
                                  @submit.native.prevent
                                  class="demo-dynamic"
                                  action="#">
                <el-form-item label="用户名："
                              prop="username">
                  <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="密码：" prop="password">
                  <el-input type="password" v-model="form.password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱：" prop="email">
                  <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="电话：">
                  <el-input v-model="form.mobile"></el-input>
                </el-form-item>
                <el-form-item label="是否可用：">
                  <el-switch v-model="form.status"></el-switch>
                </el-form-item>
                <el-form-item>
                  <el-button type="button" @click="onSubmit('form')">立即添加</el-button>
                  <el-button @click="resetForm('form')">取消</el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </div>
  </div>
</template>

<script>
import Markdown from '../../components/markdown/markdown-editor'
import initData from '../../markData.js'
export default {
  name: 'UserAdd',
  data () {
    return {
      fileList:[],
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible: false,
      article: {
        title: '',
        tag: '',
        top: false,
        content: {}
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空'}
        ],
        password: [
          { required: true, message: '密码不能为空'}
        ],
        email: [
          { required: true, message: '邮箱不能为空'}
        ]
      },
      initData: initData,
      restaurants: [],
      form:{
          username:"",
          password:"",
          email:"",
          mobile:"",
          status:"",
        }
    }
  },
  components: {Markdown},
  methods: {
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePreview (file) {
      console.log(file)
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    resetForm(form) {
        this.$refs[form].resetFields();
    },
    onSubmit(form){
      this.$refs[form].validate((valid) => {
          if (valid) {
            this.$http.post('/api/createuser',this.form
                    //  this.qs.stringify(this.form),
                    //  {headers:{
                    //    'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'
                    //  }}
                     )
                .then(response=>{
                  this.$message({
                      type: 'success',
                      message: '创建成功'
                    });
                }).catch(error=>{console.log(error)})
          } else {
            return false;
          }
        });
    },
    change () {
      console.log(arguments[0], this.article)
      this.article.content = arguments[0]
    },
    querySearch (queryString, cb) {
      var restaurants = this.restaurants
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter (queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    loadAll () {
      return [
        { 'value': 'vue' },
        { 'value': 'node.js' }
      ]
    },
    handleSelect (item) {
      console.log(item)
    },
    submitArticle () {
      this.$axios.post('/api/addArticle', {
        data: this.article
      })
        .then(function (response) {
          console.log(response);
        })
        .catch(function (error) {
          console.log(error);
        });
    }
  },
  mounted () {
    this.restaurants = this.loadAll()
  }
}
</script>

<style lang="scss">
  .subBtn{
  width: 100px;
  margin: 0 auto;
}
</style>
